<?php

declare(strict_types=1);

namespace app\admin\controller;

use app\BaseController;
use think\facade\Db;
use app\common\model\Post\Cate as CateModel;
use app\common\model\Post\Comment as CommentModel;
use app\common\model\Post\Post as PostModel;
use app\common\model\User\Record as RecordModel;
use app\common\model\User\User as UserModel;

class Analysis extends BaseController
{
    protected $middleware = [
        \app\admin\middleware\LoginAuth::class
    ];
    // 用户统计
    public function user()
    {
        $menu = [
            'name' => '数据管理',
            'title' => '用户统计'
        ];
        if ($this->request->isAjax()) {
            $action = $this->request->param('action');

            if ($action == 'pie') {
                $sex1 = UserModel::where(['sex' => 1])->count();
                $sex2 = UserModel::where(['sex' => 2])->count();
                $sex0 = UserModel::where(['sex' => 0])->count();

                $auth1 = UserModel::where(['auth' => 1])->count();
                $auth0 = UserModel::where(['auth' => 0])->count();

                $data = [
                    'sex' => [$sex1, $sex2, $sex0],
                    'auth' => [$auth1, $auth0]
                ];

                $this->success('用户数据统计', null, $data);
            }
        }
        return view('', [
            'menu' => $menu,
        ]);
    }
    // 帖子统计
    public function post()
    {
        $menu = [
            'name' => '数据管理',
            'title' => '问答统计'
        ];
        if ($this->request->isAjax()) {
            $action = $this->request->param('action');
            if ($action == 'accept') {
                $CateList = CateModel::order('weigh', 'desc')->limit(6)->select();
                //提取名字
                $name = array_column($CateList->toArray(), 'name');
                //提取id
                $ids = array_column($CateList->toArray(), 'id');
                $Accept = [];
                $NoAccept = [];
                foreach ($ids as $cateid) {
                    // 采纳
                    $Accept[] = PostModel::where([['cateid', '=', $cateid], ['accept', 'EXP', Db::raw('IS NOT NULL')]])->count();
                    // 未采纳
                    $NoAccept[] = PostModel::where([['cateid', '=', $cateid], ['accept', 'EXP', Db::raw('IS NULL')]])->count();
                }
                //组装数据
                $data = [
                    'name' => $name,
                    'Accept' => $Accept,
                    'NoAccept' => $NoAccept
                ];
                $this->success('返回数据成功', null, $data);
            }
            if ($action == 'visit') {
                // 分类列表
                $CateList = CateModel::order('weigh', 'desc')->limit(6)->select();
                //提取名字
                $name = array_column($CateList->toArray(), 'name');
                //提取id
                $ids = array_column($CateList->toArray(), 'id');
                $visit = [];
                $comment = [];
                foreach ($ids as $cateid) {
                    // 浏览量
                    $visit[] = PostModel::where([['cateid', '=', $cateid], ['visit', 'EXP', Db::raw('IS NOT NULL')]])->count();
                    // 评论量
                    $comment[] = CommentModel::withJoin(['post'])->where([['level', '=', '0'], ['post.cateid', '=', $cateid]])->count();
                }
                //组装数据
                $data = [
                    'name' => $name,
                    'visit' => $visit,
                    'comment' => $comment
                ];
                $this->success('返回帖子数据成功', null, $data);
            }
        }
        return view('', [
            'menu' => $menu,
        ]);
    }
    // 消费记录统计
    public function record()
    {
        $menu = [
            'name' => '数据管理',
            'title' => '消费统计'
        ];
        if ($this->request->isAjax()) {
            //获取当前年份
            $year = date("Y", time());

            $TimeList = [];

            for($i=1;$i<=12;$i++)
            {
                $start = "$year-$i-01";
                $end = date("Y-m-d", strtotime("$start +1 month -1 day"));
                $TimeList[] = [$start, $end];
            }

            $state1 = ['发布'];
            $state2 = ['采纳'];
            $state3 = ['充值'];
            $state4 = ['签到'];
            $state5 = ['商品订单'];
            $state6 = ['预约订单'];
            $state7 = ['退款'];
            foreach($TimeList as $item)
            {
                $state1[] = RecordModel::whereTime('createtime', 'between', $item)->where(['state' => '1'])->count();
                $state2[] = RecordModel::whereTime('createtime', 'between', $item)->where(['state' => '2'])->count();
                $state3[] = RecordModel::whereTime('createtime', 'between', $item)->where(['state' => '3'])->count();
                $state4[] = RecordModel::whereTime('createtime', 'between', $item)->where(['state' => '4'])->count();
                $state5[] = RecordModel::whereTime('createtime', 'between', $item)->where(['state' => '5'])->count();
                $state6[] = RecordModel::whereTime('createtime', 'between', $item)->where(['state' => '6'])->count();
                $state7[] = RecordModel::whereTime('createtime', 'between', $item)->where(['state' => '-1'])->count();
            }

            //X轴
            $month = ['record'];
            for($i=1;$i<=12;$i++)
            {
                $month[] = "{$i}月份";
            }

            //组装数据
            $data = [
                $month,
                $state1,
                $state2,
                $state3,
                $state4,
                $state5,
                $state6,
                $state7,
            ];
            $this->success('消费记录统计', null, $data);
        }
        return view('', [
            'menu' => $menu,
        ]);
    }
}
